
#define _POSIX_C_SOURCE 199309
#define _BSD_SOURCE
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

void sigHandler(int sig, siginfo_t *siginfo, void *ucontext){
    printf("Recive signal %d %s\n", sig, strsignal(sig));
    printf("si_code %d, %s, si_value.sival_int(timer id) %d overrun %d \n", siginfo->si_code, (siginfo->si_code == SI_TIMER) ? "SI_TIMER": "other", siginfo->si_value.sival_int, siginfo->si_overrun);
}

int main(){
    timer_t timerid;
    struct sigaction act;
    struct itimerspec value, currentvalue;

    sigemptyset(&act.sa_mask);
    act.sa_sigaction = sigHandler;
    act.sa_flags = SA_SIGINFO;

    value.it_value.tv_sec = 7;
    value.it_value.tv_nsec = 0;
    value.it_interval.tv_nsec = 0;
    value.it_interval.tv_sec = 0;

    if(sigaction(SIGALRM, &act, NULL) == -1){
        printf("Error sigaction %d %s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }


    if(timer_create(CLOCK_REALTIME, NULL, &timerid) == -1){
        printf("Error timer_create %d %s\n", errno, strerror(errno));
       exit(EXIT_FAILURE);       
    }
    

    if(timer_settime(timerid, 0, &value, NULL) == -1){
        printf("Error timer_settime %d %s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);          
    }
    printf("Timerid %ld. Sleep and wait posix timer signal\n", (long) timerid);
    pause();

    if(timer_delete(timerid) == -1){
        printf("Error timer_delete %d %s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);          
    }

    exit(EXIT_SUCCESS);
}

